package com.zhkj.shixi.tvfilemanager.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.ArrayList;

/**
 * Created by cnk on 19-6-12.
 */

public class BaseFragment extends Fragment {
    public static final String TAG = BaseFragment.class.getCanonicalName();
    private int mPosition;
    private ArrayList<BasePager> basePagers;

    public BaseFragment() {
        init();
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }

    private void init(){
        //创建列表数组
        basePagers = new ArrayList<BasePager>();
    }

    public void add(BasePager basePager){
        basePagers.add(basePager);
    }

    /**
     * 根据位置得到对应的页面
     * @return
     */
    private BasePager getBasePager() {
        Log.i(TAG,"positon =" + mPosition);
        BasePager basePager = basePagers.get(mPosition);
        if(basePager != null&&!basePager.isInitData){
            Log.i(TAG,"base page initData");
            basePager.initData();//联网请求或者绑定数据
            basePager.isInitData = true;
        }
        return basePager;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        BasePager basePager = getBasePager();
        if(basePager != null){
            return basePager.rootView;
        }
        return null;
    }
}